﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace He.Common
{
    public class FileHelper
    {
        /// <summary>
        /// 是否存在指定文件
        /// </summary>
        /// <param name="_filepath"></param>
        /// <returns></returns>
        public static bool Exists(string _filepath)
        {
            string fullpath = PathHelper.MapPath(_filepath);
            if (File.Exists(fullpath))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除单个文件
        /// </summary>
        /// <param name="_filepath">文件相对路径</param>
        public static void DeleteFile(string _filepath)
        {
            if (string.IsNullOrEmpty(_filepath))
            {
                return;
            }
            string fullpath = PathHelper.MapPath(_filepath);
            if (File.Exists(fullpath))
            {
                File.Delete(fullpath);
            }
        }

        /// <summary>
        /// 下载远程文件并保存到服务器
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <param name="savePath"></param>
        public static void DownloadFile(string httpUrl, string savePath)
        {
            WebClient client = new WebClient();
            client.Credentials = CredentialCache.DefaultCredentials;
            client.Headers.Set("User-Agent", "Microsoft Internet Explore");
            client.DownloadFile(httpUrl, PathHelper.MapPath(savePath));
        }

        /// <summary>
        /// 检查文件后缀名是否为合法
        /// </summary>
        /// <param name="allowExts">允许的后缀名</param>
        /// <param name="fileExt">上传文件的后缀名</param>
        /// <returns></returns>
        public static bool CheckFileExt(string allowExts, string fileExt)
        {
            string[] allowExt = allowExts.Split('|');
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i].ToLower() == fileExt.ToLower()) { return true; }
            }
            return false;
        }

        /// <summary>
        /// 获取文件后缀名
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetFileExt(string fileName)
        {
            return fileName.Substring(fileName.LastIndexOf(".") + 1);
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="str"></param>
        public static void WriteLogTxt(string filePath, string str)
        {
            string absolutePath = PathHelper.MapPath(filePath);
            StreamWriter sw = new StreamWriter(absolutePath, false, Encoding.UTF8);
            try
            {
                sw.Write(str);

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                sw.Close();
                sw.Dispose();
            }
        }
        /// <summary>
        /// 生成存储路径
        /// </summary>
        /// <param name="direName"></param>
        /// <param name="category_id"></param>
        /// <returns></returns>
        public static string MakeUploadDire(string webPath, string direName, int category_id)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append(webPath + "uploadfiles/");
            if (!string.IsNullOrEmpty(direName))
            {
                strTemp.Append(direName + "/");
            }
            if (category_id > 0)
            {
                strTemp.Append(category_id + "/");
            }
            string dire = strTemp.ToString();
            if (!Directory.Exists(PathHelper.MapPath(dire)))
            {
                Directory.CreateDirectory(PathHelper.MapPath(dire));
            }
            return dire;
        }

        /// <summary>
        /// 生成文件名
        /// </summary>
        /// <param name="fileExt"></param>
        /// <returns></returns>
        public static string MakeFileName(string fileExt)
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + fileExt;
        }
    }
}
